<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @editDepts="editDepts" @addDepts="addDepts" />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      departs: [],
      company: { }, // 头部的数据
      showDialog: false, // 显示窗体
      node: null
    }
  },
  created() {
    this.getDepartments() // 调用自身的方法
    // this.AddDepts()
  },
  methods: {
    async getDepartments() {
      // debugger
      const result = await getDepartments()

      // console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      // 最根级的tree-tools中,treenode属性中没有id,id便是undefined,但是进行判断时是找不到根节点的,所以在传值时,将id属性设置为''
      // console.log(result.depts)

      // 这里定义一个空字符串 是因为他是根, 所有子节点的数据pid 都是 ''
      this.departs = tranListToTreeData(result.depts, '')
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 父组件,直接调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id)
      // console.log(this.$refs.addDept)
    }
  }
}

</script>

<style scoped>
 .tree-card{
   padding:30px 140px;
   font-size:14px;
 }
</style>
